//
//  ThreadSafeController2.m
//  TEXT
//
//  Created by liubo on 2023/9/25.
//  Copyright © 2023 刘博. All rights reserved.
//

#import "ThreadSafeController2.h"

@interface ThreadSafeController2 ()

@property (atomic, assign) NSInteger sliece;

@property (nonatomic, strong) NSLock *lock;

@end

@implementation ThreadSafeController2

- (void)viewDidLoad {
    [super viewDidLoad];
    self.lock = [[NSLock alloc] init];
    [self testThreadSafe2];
    // Do any additional setup after loading the view.
}

- (void)testThreadSafe2
{
    dispatch_queue_t queue = dispatch_queue_create("kkk", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        for (int i = 0; i < 10000; i ++) {
            [self.lock lock];
            self.sliece = self.sliece + 1;
            [self.lock unlock];
        }
    });
    dispatch_async(queue, ^{
        for (int i = 0; i < 10000; i ++) {
            [self.lock lock];
            self.sliece = self.sliece + 1;
            [self.lock unlock];
        }
    });
    
    dispatch_after(5, dispatch_get_main_queue(), ^{
        NSLog(@"哈哈值得大小%ld", self.sliece);
    });
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
